﻿using System.Web;
using Core.Library;
using DataObject;
using DataObject.Cart;
using DataObject.Comment;

namespace KMT.Handlers
{
    /// <summary>
    /// Summary description for SiteHandler
    /// </summary>
    public class SiteHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string command = "";
            if (context.Request.RequestType == "POST")
            {
                if (context.Request["command"] != null)
                {
                    command = context.Request["command"];
                }
                else
                {
                    return;
                }
            }
            else
            {
                if (context.Request.QueryString["command"] != null)
                {
                    command = context.Request.QueryString["command"];
                }
                else
                {
                    return;
                }
            }

            switch (command.ToLower())
            {
                case "addtocart":
                    AddToCart(context);
                    break;
                case "postcomment":
                    PostComment(context);
                    break;
            }   
        }

        public void AddToCart(HttpContext context)
        {
            if (context.Request[Utils.Id] != null)
            {
                var id = ConvertUtility.ToInt32(context.Request[Utils.Id]);
                if (id > 0)
                {
                    try
                    {
                        ShoppingCart.Instance.AddItem(id);
                        context.Response.ContentType = "application/json";
                        context.Response.Write("[\"success\"]");        
                    }
                    catch
                    {
                        context.Response.ContentType = "application/json";
                        context.Response.Write("[\"false\"]");        
                    }                                        
                }
            }
        }
    
        public void PostComment(HttpContext context)
        {
            if (context.Request[Utils.Id] != null && context.Request[Utils.TypeId] != null)
            {
                try
                {
                    var id       = ConvertUtility.ToInt32(context.Request[Utils.Id]);
                    var typeId   = ConvertUtility.ToInt32(context.Request[Utils.TypeId]);
                    var fullName = context.Request[Utils.FullName];
                    var email    = context.Request[Utils.Email];
                    var content  = context.Request[Utils.Content];

                    if (id > 0 && typeId > 0)
                    {
                        var o = new CommentObject();
                        o.Id        = id;
                        o.ParentId  = 0;
                        o.TypeId    = typeId;
                        o.Status    = (int)CommentType.IsWaitingForPublish;
                        o.UserName  = fullName;
                        o.Email     = email;
                        o.Avatar    = Constants.Avatar_Default;
                        o.Body      = content;

                        CommentController.Comment_Create(o);

                        context.Response.ContentType = "application/json";
                        context.Response.Write("[\"success\"]");        
                    }   
                }
                catch
                {
                    context.Response.ContentType = "application/json";
                    context.Response.Write("[\"false\"]");        
                }
            }                                
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}